import { LogUtil } from '../../Util/LogUtil';
import { _classExample } from '../Manager/BaseManager';

/* eslint-disable */
export enum HandleCode {
  /* mark{0} */
  MIN = 0,
  /** 登录 */
  MAIN_SIGNIN,
  /** 获取竞技场排名 */
  ARENA_LEADERBOARD,
  /** 上传竞技场排名 */
  ARENA_LEADERBOARD_ADD,
  /** 更新竞技场数据 */
  ARENA_LEADERBOARD_UPDATE,
  /** 获取玩家排名 */
  ARENA_LEADERBOARD_PLAYER,
  /** 获取竞技场排名分页 */
  ARENA_LEADERBOARD_PAGE,
  /** 更新排行榜分数 */
  ARENA_LEADERBOARD_WINSCORE,
  /** 创建用户云存档 */
  USERS_CREATE,
  /** 获取用户云存档数据 */
  USERS_USER,
  /** 更新用户云存档数据 */
  USERS_UPDATE,
  /** 更新用户云存档数据缓存 */
  USERS_UPDATEINFOCACHE,
  /** 获取用户信息 */
  USERS_USERINFO,
  /* mark{1} */
  MAX,
}

export const Handler: Map<string, HandlerBase> = new Map();
export function registerHandler(name: string) {
  return function <T extends HandlerBase>(target: _classExample<T>) {
    if (Handler.has(name)) {
      LogUtil.e(`Handler ${name} 重复定义`);
    }
    const hander = new target();
    hander.className = name;
    hander.instance = hander;
    Handler.set(name, hander);
  };
}

export abstract class HandlerBase {
  handles: [HandleCode, (...args: any[]) => void][];
  className: string;
  private _instance: this;
  static Ins<T extends HandlerBase>(this: _classExample<T>, name): T {
    if (!Handler.has(name)) {
      LogUtil.e(`Handler ${name} 未定义, 使用registerHandler注册`);
    }
    return Handler.get(name) as T;
  }

  get instance(): this {
    return this._instance;
  }

  set instance(target: this) {
    this._instance = target;
  }
}
